!function (e, $, mui) {
    "use strict";
	
	var config = {
		city: city
	};
	// 定义页面
	var p = function(config) {
		var t = this;
		t.config = config;
		t.userInfo = null;
		t.formData = {
			start: '',
			end: '',
			start_longitude: '',
			start_latitude: '',
			end_longitude: '',
			end_latitude: '',
			start_time: '',
			num: 0,
		};
		t.selector = {
			map_search_input: 'address'
		};
		// 地图选点回调函数
		t.mapCallback = function (position) {}
		t.element = {
			start: $('.mui-content .start'),
			end: $('.mui-content .end'),
			start_time: $('.mui-content .start_time'),
			num: $('.mui-content .num'),
			submit: $('.mui-submit'),
			map_search_container: $('#map-search-container'),
			map_search_input: $('#address'),
			current_location: $('.current-location')
		};
		t.init();
	};
	
	var pt = p.prototype;
	
	// 页面初始化
	pt.init = function () {
		var t = this;
		t.userInit();
		t.listen();
	};
	
	// 用户信息初始化
	pt.userInit = function() {
		var t = this;
		t.userInfo = getLocalUserInfo();
		
		if(!t.userInfo.user_realname || !t.userInfo.mobile) {
			return mui.alert('请完善个人信息后申请', '温馨提示', '去完善', function () {
				location.href = '../user/edit.html';
			});
		}
	};
	
	// 页面监听
	pt.listen = function () {
		var t = this;
			
    	var dtPicker = new mui.DtPicker({
	    	type:'datetime',
	    	beginYear: (new Date()).getYear() - 80
    	});
		// 选择出行时间
		t.element.start_time.parent().on('tap', function () {
		    dtPicker.show(function (item) {
		    	t.element.start_time.text(item.text);
		    	t.formData.start_time = item.text;
		    });
	    });
	    
	    // 选择起点
	    t.element.start.parent().on('tap', function () {
	    	t.mapCallback = function (position) {
	    		console.log(position);
	    		t.formData.start = position.address;
	    		t.formData.start_longitude = position.location.lng;
	    		t.formData.start_latitude = position.location.lat;
	    		t.element.start.text(t.formData.start);
	    	};
	    	t.element.map_search_container.removeClass('mui-hidden');
	    });
	    
	    // 选择起点
	    t.element.end.parent().on('tap', function () {
	    	t.mapCallback = function (position) {
	    		console.log(position);
	    		t.formData.end = position.address;
	    		t.formData.end_longitude = position.location.lng;
	    		t.formData.end_latitude = position.location.lat;
	    		t.element.end.text(t.formData.end);
	    	};
	    	t.element.map_search_container.removeClass('mui-hidden');
	    });
		
    	// 提交
		t.element.submit.on('tap', function () {
			var formData = t.formData;
			formData.num = t.element.num.val();
			if(!formData.start) {
				return mui.toast('请选择起点');
			}
			if(!formData.end) {
				return mui.toast('请选择终点');
			}
			if(!formData.start_time) {
				return mui.toast('请选择出发时间');
			}
			if(formData.num < 1) {
				return mui.toast('请输入出行人数');
			}
			baseApi.post(
				'taxi/apply',
				formData,
				function (result) {
					location.href = 'taxi_detail.html?id='+result;
				}
			);
			
		});
		
		// 地图地点搜索
		//设置DomLibrary，jQuery或者Zepto
		AMapUI.setDomLibrary($);
		
		//加载PoiPicker，loadUI的路径参数为模块名中 'ui/' 之后的部分
		AMapUI.loadUI(['misc/PoiPicker'], function(PoiPicker) {
		  var poiPicker = new PoiPicker({
			  city: 'auto',
		      input: t.selector.map_search_input //输入框id
		  });
		  //监听poi选中信息
		  poiPicker.on('poiPicked', function(poiResult) {
		    //用户选中的poi点信息
		    console.log(poiResult);
		    t.mapCallback(poiResult.item);
		    t.element.map_search_container.addClass('mui-hidden');
		    t.element.map_search_input.val('');
		  });
		});
	}
	
	// 页面初始化
	e.page = new p(config);

}(window, $, mui);